#!/bin/bash
# zeping lai
# www.linuxhub.org
# chmod +x /etc/init.d/uwsgi_opman
# chkconfig uwsgi_opman on

### BEGIN INIT INFO
# Provides:          uwsgi
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the uwsgi  server
# Description:       starts uwsgi using start-stop-daemon
### END INIT INFO

NAME=uwsgi_opman
UWSGI=/data/app/opman/venv/bin/uwsgi
UWSGI_INI=/data/app/opman/uwsgi.ini

case "$1" in
	start)
		echo -n "Starting $NAME...."

		psid=`ps aux | grep -v "grep" | grep "$UWSGI $UWSGI_INI" | wc -l`
		if [ $psid -gt 0 ];then
		     echo " already running."
		     exit 1
		fi

		$UWSGI $UWSGI_INI
		if [ "$?" != 0 ] ; then
			echo " failed"
			exit 1
		else
			echo " done"
		fi
	;;

	stop)
		echo -n "Stoping $NAME..."
	        
		psid=`ps aux | grep -v "grep" | grep "$UWSGI $UWSGI_INI" | wc -l`
                if [ $psid -eq 0 ];then
		      echo " is not running."
		      exit 1
                fi


		ps -efl | grep -v grep |grep "$UWSGI $UWSGI_INI" | awk '{print $4}'|xargs kill -s 9

		if [ "$?" != 0 ] ; then
			echo " failed"
			exit 1
		else
			echo " done"
		fi
	;;

	status)
                psid=`ps aux | grep -v "grep" | grep "$UWSGI $UWSGI_INI" | wc -l`
                if [ $psid -gt 0 ];then
                     echo " already running."
		else
		     echo " is stopped"	   
                fi

	;;

	restart)
	        $0 stop
                sleep 1
                $0 start
        ;;

	 *)
                echo "Usage: $0 {start|stop|restart|status}"
                exit 1
        ;;
esac 
